package com.taoqi.service.common.synsJob;

import com.alibaba.fastjson.JSONObject;
import com.taoqi.service.common.HttpClientUtils;
import org.apache.commons.lang3.StringUtils;
import org.apache.http.impl.client.CloseableHttpClient;
import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;

import java.io.FileWriter;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.concurrent.Callable;

/**
 * Created by TQ on 2017/6/26.
 */
public class SynsJobCall implements Callable<String> {

    private CloseableHttpClient client = null;
    private List<String> url = null;
    private FileWriter fw = null;

    public SynsJobCall(CloseableHttpClient client, List<String> url, FileWriter fw) {
        this.client = client;
        this.url = url;
        this.fw = fw;
    }

    public String call() throws Exception {
        jyfkWrite2File(fw, url);
        return "666";
    }

    public void jyfkWrite2File(FileWriter fw, List<String> reqUrl) {
        Iterator<String> it = reqUrl.iterator();
        while (it.hasNext()) {
            String url = it.next();
            if (StringUtils.isNotBlank(url)) {
                jyfkWrite2File(fw, url);
            }
        }
    }

    public void jyfkWrite2File(FileWriter fw, String reqUrl) {
        //"http://www.zuchefk.com/query/HMD.aspx?sfzh=" + id
        String resDetail = HttpClientUtils.get(reqUrl, null, null, 0, 0, 0,
                HttpClientUtils.httpclient);
        if (StringUtils.isBlank(resDetail)) {
            return;
        }
        Document doc2 = Jsoup.parse(resDetail, "UTF-8");
        Map<String, Object> result = new HashMap();
        result.put("name", doc2.select("#ctl00_cphMain_xm").text());//姓名
        result.put("xsje", doc2.select("#ctl00_cphMain_CJYY").text());//悬赏金额
        Iterator<Element> it = doc2.select("input[name=ctl00$cphMain$RYLB]").iterator();
        String jklb = "";
        while (it.hasNext()) {
            Element element = it.next();
            if (element.hasAttr("checked")) {
                jklb = element.siblingNodes().get(0).childNodes().get(0).toString();
                break;
            }
        }
        result.put("jklb", jklb);//进库类别
        result.put("sfzh", doc2.select("#ctl00_cphMain_sfzh").text());//身份证号
        result.put("csrq", doc2.select("#ctl00_cphMain_CSRQ").text());//出生日期
        result.put("xb", doc2.select("ctl00_cphMain_xb [selected]").text());//性别
        result.put("mz", doc2.select("#ctl00_cphMain_mz [selected]").text());//名族
        result.put("dz", doc2.select("#ctl00_cphMain_dz").text());//住址
        result.put("jdsj", doc2.select("#ctl00_cphMain_JDSJ").text());//建档时间
        result.put("ssgs", doc2.select("#ctl00_cphMain_SSGS").text());//所属公司
        result.put("lxdh", doc2.select("#ctl00_cphMain_LXDH").text());//联系电话
        result.put("cjyy", doc2.select("#ctl00_cphMain_CJLY").text());//创建原因
        result.put("bz", doc2.select("#ctl00_cphMain_BZ").text());//备注
        result.put("cjyy", doc2.select("#ctl00_cphMain_myLabel").text());//查询记录
        if (StringUtils.isNotBlank(result.get("sfzh") + "")) {
            String str = JSONObject.toJSONString(result);
            write2File(fw, str);
        }

        //表格
            /*Elements hang = doc2.select(".grid tr");
            Iterator<Element> it = hang.iterator();
            while (it.hasNext()) {
                Elements lie = it.next().getAllElements().select("td");
                Iterator<Element> itLie = lie.iterator();
                while (itLie.hasNext()) {
                    String txt = itLie.next().text();
                    System.out.print(txt);
                }
                System.out.println();
            }*/
    }

    public synchronized void write2File(FileWriter fw, String str) {
        try {
            fw.write(str);
            fw.write("\n");
            fw.flush();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}
